package com.xc.smart.security.handler;


import com.xc.smart.entity.LoginUser;
import com.xc.smart.jwt.JWTService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.util.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 自定义退出处理类 返回成功
 * 
 * @author ruoyi
 */
@Configuration
public class LogoutSuccessHandlerImpl implements LogoutSuccessHandler {

    @Autowired
    JWTService jwtService;

    /**
     * 退出处理
     * 
     * @return
     */
    @Override
    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication)
            throws IOException, ServletException
    {
        LoginUser loginUser = jwtService.getLoginUser(request);
        if (!StringUtils.isEmpty(loginUser))
        {
            String userName = loginUser.getUser().getUsername();
            // 删除用户缓存记录
            jwtService.delLoginUser(loginUser.getToken());
            // 记录用户退出日志
            System.out.println("退出成功");
        }
        System.out.println("退出成功");
    }
}
